//
//  BranchOpenPartyOrganizationViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "BranchOpenPartyOrganizationViewController.h"
#import "BranchOpenOrgDynimicModel.h"
#import "BranchOpenOrganizationHeaderModel.h"
#import "BranchOpenOrganizationHeaderView.h"
#import "BranchOpenPartyOrgDynamicTableViewCell.h"
#import "HomeSectionHeaderView.h"
#import "BranchPartyOrgizationModel.h"
#import "PartyOrgRequestViewController.h"
#import "PartyOrgResourseViewController.h"
#import "LPWKHasCommentAndHeadLineViewController.h"
#import "LPDCDetailModel.h"
#import "BranchPartyOrganizeNewsListViewController.h"
#import "MyBranchDynamicViewController.h"
#import "LPCommonFistActListViewController.h"
@interface BranchOpenPartyOrganizationViewController ()<UITableViewDelegate,UITableViewDataSource,BranchOpenOrganizationHeaderDelegate,HomeSectionHeaderDelegate,BranchOpenPartyOrgDynamicTableDelegate>
/**  tableView  */
@property (strong, nonatomic) UITableView  *tableView;
/**  支部动态数据  */
@property (strong, nonatomic) NSArray  *dynimicArray;
/**  头部诗句  */
@property (strong, nonatomic) BranchOpenOrganizationHeaderModel  *headerModel;

@end

@implementation BranchOpenPartyOrganizationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.model.DEPT_NAME;
    
    [self getDataSource];
    
}
#pragma mark - 数据
- (void)getDataSource{
    __weak typeof(self) weakSelf = self;

     [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"UNIT_ID"] = self.model.ID;
    paramer[@"NEED_NEWS"] = @"YES";
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionGetDeptMsgfindDeptById] Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.headerModel = [BranchOpenOrganizationHeaderModel yy_modelWithJSON:[object objectForKey:@"DEPT_MSG"]];
            weakSelf.dynimicArray = [NSArray yy_modelArrayWithClass:[BranchOpenOrgDynimicModel class] json:[object objectForKey:@"NEWS"]];
            [weakSelf layoutUI];
        }else{
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf getDataSource];
            }];
        }
    } Fail:^(id object) {
        [YJProgressHUD hide];
        [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
            [weakSelf getDataSource];
        }];
    }];
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    CGFloat headerHeight = 0.0;
    CGFloat height = [CalculateLabelHeight CalculateLabelHeightWithString:self.headerModel.DEPTCONTACTS_ADDRESS font:16 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]].height;
    if (height > 20 * [UIScreen zj_screenHScale]) {
        headerHeight = 210 * [UIScreen zj_screenHScale] + self.headerModel.HEIGHT + height;
    }else{
        headerHeight =  230 * [UIScreen zj_screenHScale] + self.headerModel.HEIGHT;
    }
    BranchOpenOrganizationHeaderView *header = [[BranchOpenOrganizationHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, headerHeight)];
    header.model = self.headerModel;
    header.delegate = self;
    self.tableView.tableHeaderView = header;
}


#pragma mark - 私有方法

#pragma mark - HomeSectionHeaderDelegate
- (void)homeSectionHeaderWithVeiw:(HomeSectionHeaderView *)view ClickSection:(NSInteger)clicksection{
    /**  查看更多  */
    MyBranchDynamicViewController *vc = [MyBranchDynamicViewController new];
    vc.ID = self.headerModel.ID;
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - BranchOpenOrganizationHeaderDelegate
- (void)BranchOpenOrganizationHeaderTap_index:(NSInteger)index{
    switch (index) {
        case 0:
        {
            MyBranchDynamicViewController *vc = [MyBranchDynamicViewController new];
            vc.ID = self.headerModel.ID;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            PartyOrgResourseViewController *vc = [PartyOrgResourseViewController new];
            vc.UNITID = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
        }

            break;

        case 2:
        {
            PartyOrgRequestViewController *vc = [PartyOrgRequestViewController new];
            vc.headLine = @"需求";
            vc.UNITID = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        case 3:
        {
            LPCommonFistActListViewController *vc = [LPCommonFistActListViewController new];
            vc.type = @"活动";
            vc.DEPTID = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        default:
            break;
    }
}

#pragma mark - BranchOpenPartyOrgDynamicTableDelegate
- (void)BranchOpenPartyOrgDynamicTable_section:(NSInteger)sction index:(NSInteger)index{
    LPDCDetailModel *report = [LPDCDetailModel new];
    BranchOpenOrgDynimicModel *model = self.dynimicArray[index];
    report.ID = model.ID;
    report.URL = model.URL;
    report.TITLE = model.TITLE;
    LPWKHasCommentAndHeadLineViewController *vc = [LPWKHasCommentAndHeadLineViewController new];
    vc.model = report;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dynimicArray.count == 0) {
        return 0;
    }else{
        return 1;

    }
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dynimicArray.count == 0) {
        return 0;
    }else{
        return 190 * [UIScreen zj_screenHScale];
        
    }

}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    BranchOpenPartyOrgDynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[BranchOpenPartyOrgDynamicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.delegate = self;
    cell.data = self.dynimicArray;
    cell.section = indexPath.section;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeSectionHeaderView *header = [[HomeSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale])];
    header.titleLabel.text = @"支部动态";
    header.delegate = self;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * [UIScreen zj_screenHScale];
}
#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}

- (BranchOpenOrganizationHeaderModel *)headerModel{
    if (!_headerModel) {
        _headerModel = [BranchOpenOrganizationHeaderModel new];
    }
    return _headerModel;
}

@end
